﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace BtoBSWT315.User
{
    public partial class Add_Job : System.Web.UI.Page
    {
        private int Gsid;
        private int hybz;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(showname.Text) || String.IsNullOrEmpty(content.Text) || String.IsNullOrEmpty(company.Text))
            {
                Response.Write("<script>alert('请认真填写招聘信息');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
            }
        
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();

            bool IsVIP = classdb.CheckVIP(Session["user"].ToString());

            //（1）添加是否是付费会员验证
            string checkCountSql = "select count(*) as [id] from [job] inner join qyml on job.gsid=qyml.id where qyml.user='" + Session["user"].ToString() + "'";

            if (!IsVIP && classdb.GetCount(checkCountSql) > 10)
            {
                Response.Write("<script>alert('您已经添加了10条招聘信息！如果您想添加更多招聘信息，请先升级为VIP会员！');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
                return;
            }

            else
            {
                string sqlGsid = "select id,hybz from qyml where [user]='" + Session["user"] + "'";
                DataTable dataTable = null;

                dataTable = classdb.GetTables(sqlGsid);
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    foreach (DataRow row in dataTable.Rows)
                    {
                        Gsid = Convert.ToInt32(row["id"].ToString());
                        hybz = Convert.ToInt32(row["hybz"].ToString());
                    }
                }

                string insertSql = "insert into job (showname,type,period,content,linkman,zrs,company,address,phone,web,mail,country,dateandtime,gsid,hybz)values('" + showname.Text.Trim() + "','" + type.SelectedValue + "','" + period.SelectedValue + "','" + content.Text.Trim() + "','" + linkman.Text.Trim() + "','" + zrs.Text.Trim() + "','" + company.Text.Trim() + "','" + address.Text.Trim() + "','" + phone.Text.Trim() + "','" + web.Text.Trim() + "','" + mail.Text.Trim() + "','" + country.Text.Trim() + "','" + DateTime.Now.ToShortDateString() + "','" + Gsid + "','" + hybz + "')";

                if (classdb.GetNonResult(insertSql) != 0)
                {
                    Response.Write("<script>alert('添加成功');</script>");
                    Response.Write("<script>history.go(-2);</script>");
                    Response.End();
                }
                else
                {
                    Response.Write("<script>alert('操作失败！请重新填写。');</script>");
                    Response.Write("<script>history.go(-1);</script>");
                    Response.End();
                }
            }
        }
    }
}